package com.youxuan.ssyx.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youxuan.ssyx.acl.service.AdminService;
import com.youxuan.ssyx.acl.service.RoleService;
import com.youxuan.ssyx.common.result.Result;
import com.youxuan.ssyx.common.utils.MD5;
import com.youxuan.ssyx.model.acl.Admin;
import com.youxuan.ssyx.vo.acl.AdminQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/acl/user")

public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    // 获取所有角色，根据用户 id 查询用户分配角色列表
    @ApiOperation("获取用户角色")
    @GetMapping("toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId) {
        Map<String, Object> map = roleService.getRoleByAdminId(adminId);
        return Result.ok(map);
    }

    @ApiOperation("用户角色分配")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long adminId, @RequestParam Long[] roleId) {
        roleService.saveAdminRole(adminId,roleId);
        return Result.ok(null);
    }

    // 1 用户列表
    @ApiOperation("用户列表")
    @GetMapping("{current}/{limit}")
    public Result list(@PathVariable Long current, @PathVariable Long limit, AdminQueryVo adminQueryVo) {
        Page<Admin> pageParam = new Page<Admin>(current, limit);
        IPage<Admin> pageModel = adminService.selectPageUser(pageParam, adminQueryVo);
        return Result.ok(pageModel);
    }
    // 2 id 查询用户
    @ApiOperation("根据 ID 查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }
    // 3 添加用户
    @ApiOperation("添加用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin) {
        // 获取输入的密码
        String password = admin.getPassword();

        // 对输入的密码进行加密
        String passwordMD5 = MD5.encrypt(password);

        // 设置到 admin 对象中
        admin.setPassword(passwordMD5);

        // 调用添加用户处理
        boolean res = adminService.save(admin);
        if (res) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }
    // 4 修改用户
    @ApiOperation("修改用户")
    @PutMapping("update")
    public Result update(@RequestBody Admin admin) {
        boolean res = adminService.updateById(admin);
        if (res) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }
    // 5 删除用户
    @ApiOperation("根据 id 删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean res = adminService.removeById(id);
        if (res) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }
    // 6 批量删除
    @ApiOperation("批量删除用户")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean res = adminService.removeByIds(idList);
        if (res) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }
}
